/**
 * chinagoods.com
 * Copyright (c) 2020-2024 All Rights Reserved.
 */
package com.ruoyi.system.biz.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.ChannelEnum;
import com.ruoyi.common.exception.BusiException;
import com.ruoyi.system.biz.UserRateBizService;
import com.ruoyi.system.domain.Bank;
import com.ruoyi.system.domain.UserRate;
import com.ruoyi.system.dto.UserSetRateDto;
import com.ruoyi.system.service.BankService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.UserRateService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yangpeng
 * Created by on 2024-01-23 12:58
 */
@Service
public class UserRateBizServiceImpl implements UserRateBizService {
    @Resource
    private UserRateService userRateService;
    @Resource
    private ISysUserService userService;

    @Resource
    private BankService bankService;

    @Transactional
    @Override
    public void setRate(Long parentId, UserSetRateDto userSetRateDto) {
        Long memberUserId = userSetRateDto.getUserId();
        UserRate userRate = new UserRate();
        userRate.setUserId(parentId);
        //查询上级的费率数据
        List<UserRate> list = userRateService.list(userRate);
        if (CollectionUtil.isEmpty(list)) {
            throw new BusiException("请联系上级配置通道权限");
        }

//        Field[] fields = userSetRateDto.getClass().getFields();
//        if (list.size() != fields.length - 1) {
//            throw new BusiException("请联系上级增加通道权限");
//        }

        List<UserRate> result = new ArrayList<>();
        BigDecimal zfbUidRate = userSetRateDto.getZfbUidRate();
        UserRate zfbUid = setAndCheck(memberUserId, zfbUidRate, ChannelEnum.UID, list);

        BigDecimal zfbRate = userSetRateDto.getZfbRate();
        UserRate zfb = setAndCheck(memberUserId, zfbRate, ChannelEnum.ZFB, list);

        BigDecimal tbRate = userSetRateDto.getTbRate();
        UserRate tb = setAndCheck(memberUserId, tbRate, ChannelEnum.TB, list);

        BigDecimal szRmbRate = userSetRateDto.getSzRmbRate();
        UserRate szRmb = setAndCheck(memberUserId, szRmbRate, ChannelEnum.SZ_RMB, list);

        BigDecimal zzmRate = userSetRateDto.getZzmRate();
        UserRate zzm = setAndCheck(memberUserId, zzmRate, ChannelEnum.ZFB_ZZ, list);


        BigDecimal smUidbRate   = userSetRateDto.getSmUidRate();
        UserRate smUidb = setAndCheck(memberUserId, smUidbRate, ChannelEnum.SM_UID, list);



//        BigDecimal zfbXhbRate = userSetRateDto.getZfbXhbRate();
//        UserRate zfbXhb = setAndCheck(memberUserId, zfbXhbRate, ChannelEnum.ZFB_XHB, list);


        // BigDecimal bankRate = userSetRateDto.getBankRate();
        // UserRate bank = setAndCheck(memberUserId, bankRate, ChannelEnum.BANK, list);

        result.add(szRmb);
        result.add(zfb);
        result.add(zfbUid);
        result.add(tb);
        result.add(zzm);
        result.add(smUidb);
//        result.add(zfbXhb);
        CollectionUtil.removeNull(result);

        if (CollectionUtil.isNotEmpty(result)) {
            userRateService.saveOrUpdateBatch(result);
        }


    }

    private UserRate setAndCheck(Long memberUserId, BigDecimal rate, ChannelEnum channelEnum, List<UserRate> list) {
        if (rate == null) {
            return null;
        }

        BigDecimal szRmbRate = rate;
        UserRate szRmb = getUserRate(memberUserId, channelEnum);
        UserRate szRmb1 = userRateService.getDomain(szRmb);
        szRmb.setRate(szRmbRate);
        if (szRmb1 != null) {
            szRmb.setId(szRmb1.getId());
        }
        //比较数字人民币费率
        check(szRmb, list);
        return szRmb;
    }

    private UserRate getUserRate(Long memberUserId, ChannelEnum channelEnum) {
        UserRate szRmb = new UserRate();
        szRmb.setUserId(memberUserId);
        szRmb.setChannelCode(channelEnum.getCode());
        szRmb.setChannelName(channelEnum.getName());
        return szRmb;
    }

    private void check(UserRate szRmb, List<UserRate> list) {
        boolean index = false;

        for (int i = 0; i < list.size(); i++) {
            UserRate userRate = list.get(i);
            if (StrUtil.equals(szRmb.getChannelCode(), userRate.getChannelCode())) {
                index = true;

                if (szRmb.getRate().compareTo(userRate.getRate()) > 0) {
                    throw new BusiException(szRmb.getChannelName() + "费率不能大于本人费率");
                }
            }
        }
        if (!index) {
            throw new BusiException(szRmb.getChannelName() + "无权限");
        }
    }

    @Override
    public void addRate(Long parentId, Long userId) {
        UserRate userRate = new UserRate();
        userRate.setUserId(parentId);
        List<UserRate> list = userRateService.list(userRate);
        if (CollectionUtil.isEmpty(list)) {
            return;
        }
        List<UserRate> collect = list.stream().map(u -> {
            u.setUserId(userId);
            u.setCreateTime(null);
            u.setUpdateTime(null);
            return u;
        }).collect(Collectors.toList());

        userRateService.addDomainBatch(list);
    }


    public void clean() {
        List<UserRate> userRateList = new ArrayList<>();
        //查询所有用户
        List<SysUser> userList = userService.list(new SysUser());

        for (int i = 0; i < userList.size(); i++) {//处理某个用户的费率
            SysUser sysUser = userList.get(i);
            BigDecimal zfbRate = sysUser.getZfbRate();
            //支付宝转账
            UserRate zfbUserRate = new UserRate();
            zfbUserRate.setRate(zfbRate);
            zfbUserRate.setUserId(sysUser.getUserId());
            zfbUserRate.setChannelCode(ChannelEnum.ZFB.getCode());
            zfbUserRate.setChannelName(ChannelEnum.ZFB.getName());
            userRateList.add(zfbUserRate);

            //支付宝uid
            UserRate zfbUidRate = new UserRate();
            zfbUidRate.setRate(zfbRate);
            zfbUidRate.setUserId(sysUser.getUserId());
            zfbUidRate.setChannelCode(ChannelEnum.UID.getCode());
            zfbUidRate.setChannelName(ChannelEnum.UID.getName());
            userRateList.add(zfbUidRate);
            //数字人民币
            UserRate szRmbRate = new UserRate();
            szRmbRate.setRate(sysUser.getBankRate());
            szRmbRate.setUserId(sysUser.getUserId());
            szRmbRate.setChannelCode(ChannelEnum.SZ_RMB.getCode());
            szRmbRate.setChannelName(ChannelEnum.SZ_RMB.getName());
            userRateList.add(szRmbRate);


            userRateService.addDomainBatch(userRateList);
            userRateList = new ArrayList<>();
            //查询收款方式
            Bank bank = new Bank();
            bank.setUserId(sysUser.getUserId());
            List<Bank> bankList = bankService.findList(bank);
            if (CollectionUtil.isEmpty(bankList)) {
                continue;
            }
            for (int j = 0; j < bankList.size(); j++) {
                bank = bankList.get(j);
                if (StrUtil.equals(bank.getChannelCode(), ChannelEnum.UID.getCode())) {//如果是支付宝uid 加一条支付宝转账
                    Bank bank1 = new Bank();
                    BeanUtil.copyProperties(bank, bank1);
                    bank1.setId(null);
                    bank1.setCreateTime(null);
                    bank1.setUpdateTime(null);
                    bank1.setZfbUserId(null);
                    bank1.setChannelCode(ChannelEnum.ZFB.getCode());
                    bankService.addDomain(bank1);
                }
            }

        }


    }
}